package com.example.alarm.mvvm.fragment

import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.RecyclerView
import com.example.alarm.R
import com.example.alarm.mvvm.adapter.AlarmListAdapter
import com.example.alarm.mvvm.bean.AlarmTypeBean
import com.gsafety.libraryktx.bean.WarnData
import com.example.login.mvvm.viewmodel.AlarmViewModel
import com.gsafety.libraryktx.base.BaseActivity
import com.gsafety.libraryktx.base.BaseFragment
import com.gsafety.libraryktx.bean.LoadState
import com.gsafety.libraryktx.config.Constant
import com.gsafety.libraryktx.expand.create
import com.gsafety.libraryktx.expand.toast
import com.gsafety.libraryktx.util.RecyclerViewHelper
import com.gsafety.libraryktx.widget.CommonRefreshView
import com.jeremyliao.liveeventbus.LiveEventBus
import kotlinx.android.synthetic.main.alarm_list_fragment_layout.*

/**预警列表
 * @author : lj
 * @date : 2021/1/6 10:41
 */
class AlarmListFragment: BaseFragment(), CommonRefreshView.RefreshListener {
    var datas = ArrayList<WarnData.RecordsBean>()
    var mAdapter: AlarmListAdapter? = null
    private lateinit var mv: AlarmViewModel
    var pageSize = 10
    var current = 1
    var type = Constant.NO_DEAL_CODE
    var currentAttendPos = 0
    var mEtSearch:EditText? = null
    override fun initData() {
        var rv_msg=view?.findViewById<RecyclerView>(R.id.rv_msg)
        mAdapter = AlarmListAdapter(datas)
        RecyclerViewHelper.initRecyclerViewV(mContext, rv_msg!!, false, mAdapter)

        mv.loadState.observe(this, Observer {
            when (it) {
                is LoadState.Success -> {
                    crv_common_refresh.finishRefresh()
                    if(datas.isNullOrEmpty()&&mv.loadValue.value != null){
                        LiveEventBus.get(Constant.EventBus_KEY_Alarm_Title).post(AlarmTypeBean(mv.loadValue.value?.total.toString(),type))
                    }
                    mv.loadValue.value.let {it->
                        datas.addAll(it?.records!!)
                        mAdapter?.setNewData(datas)
                    }
                }
                is LoadState.Fail -> {
                    (activity as BaseActivity).toast(it.msg)
                    crv_common_refresh.finishRefresh()
                }
            }
        })

        mEtSearch?.setOnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                onRefresh()
            }
            false
        }

        mAdapter?.onItemAttentionClickListener = object:AlarmListAdapter.OnItemAttentionClickListener{
            override fun onItemAttendClick(pos: Int) {
                var item = datas.get(pos)
                currentAttendPos = pos
                if(item.attended){
                    mv.cancelAttention(item.pid!!)
                }else{
                    mv.setAttention(item.pid!!)
                }
            }
        }

        mv.setAttendValue.observe(this, Observer {
            if (mv.setAttendValue.value == true) {
                datas.get(currentAttendPos).attended = true
                mAdapter?.notifyDataSetChanged()
                Toast.makeText(activity, "关注成功！", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(activity, "关注失败！", Toast.LENGTH_SHORT).show()
            }
        })

        mv.cancelAttendValue.observe(this, Observer {
            if (mv.cancelAttendValue.value == true) {
                datas.get(currentAttendPos).attended = false
                mAdapter?.notifyDataSetChanged()
                Toast.makeText(activity, "取消关注成功！", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(activity, "取消关注失败！", Toast.LENGTH_SHORT).show()
            }
        })
    }

    override fun onResume() {
        super.onResume()
        crv_common_refresh.autoRefresh()
    }

    override fun getLayoutId(): Int {
        return R.layout.alarm_list_fragment_layout
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mv = AlarmViewModel()
        mv = create(AlarmViewModel::class)
    }

    override fun initView(view: View?) {
        crv_common_refresh.mRefreshListener = this
        type = arguments?.getString(DATA_TYPE)!!
        mEtSearch = view?.findViewById<EditText>(R.id.et_search)
    }


    companion object {
        private const val DATA_TYPE = "data_type_key"
        fun newInstance(type: String): AlarmListFragment {
            var fragment = AlarmListFragment()
            var args = Bundle()
            args.putString(DATA_TYPE, type)
            fragment.arguments = args
            return fragment
        }
    }

    override fun onRefresh() {
        current = 1
        datas.clear()
        mv.requestWarnList(pageSize,current,type,mEtSearch?.text.toString())
    }

    override fun onLoadMore() {
        current++
        mv.requestWarnList(pageSize,current,type,mEtSearch?.text.toString())
    }
}